package com.sylli.oeuf.server.db.sql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;

import com.sylli.oeuf.server.exception.BasedException;
import com.sylli.oeuf.server.exception.ExceptionCause;
import com.sylli.oeuf.server.object.transferable.AccountTO;

public class MySQLAccount extends MySQLBasedProvider<AccountTO> {

	public MySQLAccount () {
		interactTableName = "account";
	}
	
	@Override
	protected PreparedStatement convertObjectToInsertQuery(AccountTO obj)
			throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"INSERT INTO " + interactTableName 
					+ " (username, password, email, flag)"
					+ " VALUES (?,?,?,?)", 
					Statement.RETURN_GENERATED_KEYS);
			ps.setString(1, obj.getUsername());
			ps.setString(2, obj.getPassword());
			ps.setString(3, obj.getEmail());
			ps.setInt(4, obj.getFlag());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryCreationFail, e);
		}
	}

	@Override
	protected PreparedStatement convertObjectToUpdateQuery(AccountTO obj)
		throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"UPDATE " + interactTableName 
					+ " SET username=?, password=?, email=?, flag=? WHERE account_id=?;");
			ps.setString(1, obj.getUsername());
			ps.setString(2, obj.getPassword());
			ps.setString(3, obj.getEmail());
			ps.setInt(4, obj.getFlag());
			ps.setInt(5, obj.getAccountId());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryUpdateFail, e);
		}
	}

	@Override
	protected AccountTO convertResultSetToObject(ResultSet rs)
			throws BasedException {
		try {
			AccountTO ato = new AccountTO();
			rs.first();
			ato.setAccountId(rs.getInt(1));
			ato.setUsername(rs.getString(2));
			ato.setPassword(rs.getString(3));
			ato.setEmail(rs.getString(4));
			ato.setFlag(rs.getInt(5));
			return ato;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryAccessingFail, e);
		}
	}

	@Override
	protected Collection<AccountTO> convertResultSetToObjects(ResultSet rs)
			throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected PreparedStatement convertObjectToInsertQuery(
			Collection<AccountTO> objs) throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}
}
